package org.springblade.modules.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.mp.TenantEntity;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.dto.sugar.SugarSearchDTO;
import org.springblade.modules.admin.entity.Building;
import org.springblade.modules.admin.entity.MaintainSugar;
import org.springblade.modules.admin.entity.PlanContentStatistical;
import org.springblade.modules.admin.service.*;
import org.springblade.modules.admin.vo.sugar.*;
import org.springblade.modules.system.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zcc
 * @version 1.0
 * @date 2021/11/15 11:22
 */
@RestController
@RequestMapping("/admin/sugar")
@AllArgsConstructor
@Api(value = "数据大屏管理", tags = "数据大屏管理")
public class SugarController {

	private final SugarService sugarService;

	private final BuildingService buildingService;

	private final IPlanContentStatisticalService planContentStatisticalService;

	private final MaintainSugarService maintainSugarService;

	private final IMaintainPlanContentService maintainPlanContentService;

	/**
	 * 获取大屏设置详情
	 * @return
	 */
	@GetMapping("/detail")
	public R<MaintainSugar> detail(Integer status){
		return R.data(maintainSugarService.getOne(Wrappers.<MaintainSugar>lambdaQuery().eq(MaintainSugar::getStatus,status).eq(TenantEntity::getTenantId,AuthUtil.getTenantId()),false));
	}

	/**
	 * 获取大屏设置列表
	 * @return
	 */
	@GetMapping("/list")
	public R<List<MaintainSugar>> list(){
		return R.data(maintainSugarService.list(Wrappers.<MaintainSugar>lambdaQuery().eq(TenantEntity::getTenantId,AuthUtil.getTenantId())));
	}

	/**
	 * 大屏设置更新
	 * @param sugar
	 * @return
	 */
	@PutMapping("/update")
	public R<Boolean> update(@RequestBody MaintainSugar sugar){
		MaintainSugar entity = maintainSugarService.getOne(Wrappers.<MaintainSugar>lambdaQuery().eq(MaintainSugar::getStatus,sugar.getStatus()).eq(TenantEntity::getTenantId,AuthUtil.getTenantId()),false);
		if(ObjectUtil.isNotEmpty(entity)){
			sugar.setId(entity.getId());
		}
		return R.status(maintainSugarService.saveOrUpdate(sugar));
	}


	//	@GetMapping("/getFinishRatio")
//	public SugarR getFinishRatio(SugarSearchVO sugarSearchVO) {
//		String beginTime = "2021-10-16";
//		sugarSearchVO.setBeginTime(beginTime);
//		sugarSearchVO.setEndTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
//		SugarR sugarR = new SugarR();
//		sugarR.setData(sugarService.getFinishRatio(sugarSearchVO));
//		return sugarR;
//	}
//
//
//	@GetMapping("/getRankingList")
//	public SugarR getRankingList(SugarSearchVO sugarSearchVO) {
//		String beginTime = "2021-10-16";
//		sugarSearchVO.setBeginTime(beginTime);
//		sugarSearchVO.setEndTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
//		SugarR sugarR = new SugarR();
//		sugarR.setData(sugarService.getRankingList(sugarSearchVO));
//		return sugarR;
//	}
//
//	@GetMapping("/getTimelinessAndFinishRatio")
//	public SugarR getTimelinessAndFinishRatio(Integer type) {
//
//		SugarR sugarR = new SugarR();
//		sugarR.setData(sugarService.getTimelinessAndFinishRatio(type));
//		return sugarR;
//	}
//
//
//	@PostMapping("/getBuildingInfo")
//	@ApiOperation("获取项目信息")
//	public R<BuildingInfoVo> getBuildingInfo(@RequestBody SugarSearchDTO sugarSearchDTO) {
//
//		sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
//		sugarSearchDTO.setEndDate(new Date());
//		sugarSearchDTO.setStartDate(DateUtil.offsetDay(new Date(), -90));
//		BuildingInfoVo buildingInfo = sugarService.getBuildingInfo(sugarSearchDTO);
//		return R.data(buildingInfo);
//	}
//
//
//	@PostMapping("/getWorkPlanInfo")
//	@ApiOperation("获取排班信息")
//	public R<WorkPlanInfoVo> getWorkPlanInfo(@RequestBody SugarSearchDTO sugarSearchDTO) {
//		sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
//		sugarSearchDTO.setEndDate(DateUtil.endOfMonth(new Date()));
//		sugarSearchDTO.setStartDate(DateUtil.beginOfMonth(new Date()));
//		sugarSearchDTO.setStartTime(DateUtil.format(DateUtil.beginOfMonth(new Date()), "yyyy-MM-dd"));
//		sugarSearchDTO.setEndTime(DateUtil.format(DateUtil.endOfMonth(new Date()), "yyyy-MM-dd"));
//		WorkPlanInfoVo workPlanInfo = sugarService.getWorkPlanInfo(sugarSearchDTO);
//		return R.data(workPlanInfo);
//	}
//
	@PostMapping("/getEleVatorAndUserInfo")
	@ApiOperation("获取梯台数和维保数量")
	public R<ElevatorAndUserInfoVo> getEleVatorAndUserInfo(@RequestBody SugarSearchDTO sugarSearchDTO) {
		sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
		sugarSearchDTO.setEndDate(new Date());
		sugarSearchDTO.setStartDate(DateUtil.beginOfMonth(new Date()));
		ElevatorAndUserInfoVo eleVatorAndUserInfo = sugarService.getEleVatorAndUserInfo(sugarSearchDTO);
		return R.data(eleVatorAndUserInfo);
	}


	@PostMapping("/getElevatorAndUserInfo/pro")
	@ApiOperation("获取人梯比变化率")
	public R<Map<String,Object>> getElevatorAndUserInfoPro(@RequestBody SugarSearchDTO sugarSearchDTO){
		return R.data(sugarService.getElevatorAndUserInfoPro(sugarSearchDTO));
	}

	//
//
//	@PostMapping("/getCarryOutInfo")
//	@ApiOperation("获取执行信息")
//	public R<CarryOutVo> getCarryOutInfo(@RequestBody SugarSearchDTO sugarSearchDTO) {
//		sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
//		sugarSearchDTO.setStartTime(DateUtil.format(DateUtil.beginOfMonth(new Date()), "yyyy-MM-dd"));
//		sugarSearchDTO.setEndTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
//		CarryOutVo carryOutInfo = sugarService.getCarryOutInfo(sugarSearchDTO);
//		return R.data(carryOutInfo);
//	}
//
//	@PostMapping("/getCarryOutInfo/init")
//	@ApiOperation("获取执行信息初始化")
//	public R<CarryOutVo> getCarryOutInfoInit(@RequestBody SugarSearchDTO sugarSearchDTO) {
//		sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
//		CarryOutVo carryOutInfo = sugarService.getCarryOutInfo(sugarSearchDTO);
//		PlanContentStatistical planContentStatistical = new PlanContentStatistical();
//		BeanUtil.copyProperties(carryOutInfo, planContentStatistical);
//		DateTime dateTime = DateUtil.parse(sugarSearchDTO.getStartTime() + " 00:00:00");
//		planContentStatistical.setMonth(Convert.toInt(DateUtil.format(dateTime, "yyyyMM")));
//		planContentStatisticalService.save(planContentStatistical);
//		return R.data(carryOutInfo);
//	}
//
//
//	@PostMapping("/getCarryOutVoOrderByGroupId")
//	@ApiOperation("获取执行明细")
//	public R<List<CarryOutVo>> getCarryOutVoOrderByGroupId(Integer type) {
//		List<CarryOutVo> carryOutVoOrderByGroupId = sugarService.getCarryOutVoOrderByGroupId(type);
//		return R.data(carryOutVoOrderByGroupId);
//	}
//
//
//	@PostMapping("/getSignDetail")
//	@ApiOperation("获取考勤信息")
//	public R<SignDetailVo> getSignDetail(Integer type) {
//		SugarSearchDTO sugarSearchDTO = new SugarSearchDTO();
//		if (type == null||type==1) {
//			sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
//			sugarSearchDTO.setEndDate2(Convert.toInt(DateUtil.format(new Date(), "yyyyMMdd")));
//			sugarSearchDTO.setStartDate2(Convert.toInt(DateUtil.format(DateUtil.beginOfMonth(new Date()), "yyyyMMdd")));
//		}else{
//			sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
//			sugarSearchDTO.setEndDate2(Convert.toInt(DateUtil.format(DateUtil.endOfMonth(DateUtil.lastMonth()), "yyyyMMdd")));
//			sugarSearchDTO.setStartDate2(Convert.toInt(DateUtil.format(DateUtil.beginOfMonth(DateUtil.lastMonth()), "yyyyMMdd")));
//		}
//		SignDetailVo signDetail = sugarService.getSignDetail(sugarSearchDTO);
//		return R.data(signDetail);
//	}
//
	@PostMapping("/getBuildingMaintainDetail")
	@ApiOperation("获取项目任务信息")
	public R<BuildingMaintainDetailVo> getBuildingMaintainDetail(@RequestBody SugarSearchDTO sugarSearchDTO) {
		sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
		sugarSearchDTO.setEndDate(DateUtil.endOfMonth(new Date()));
		sugarSearchDTO.setStartDate(DateUtil.beginOfMonth(new Date()));
		BuildingMaintainDetailVo buildingMaintainDetail = sugarService.getBuildingMaintainDetail(sugarSearchDTO);
		Building building = buildingService.getById(sugarSearchDTO.getBuildingId());
		if (building != null) {
			buildingMaintainDetail.setBuildingName(building.getName());
			buildingMaintainDetail.setBuildingType(Convert.toInt(building.getType()));
		}
		return R.data(buildingMaintainDetail);
	}

	//
//
	@PostMapping("/getBuildingList")
	@ApiOperation("获取项目列表")
	public R<List<Building>> getBuildingList(@RequestBody SugarSearchDTO sugarSearchDTO) {
		sugarSearchDTO.setTenantId(AuthUtil.getTenantId());
		sugarSearchDTO.setEndDate(DateUtil.endOfMonth(new Date()));
		sugarSearchDTO.setStartDate(DateUtil.beginOfMonth(new Date()));
		List<Building> list = buildingService.getList(sugarSearchDTO);
		return R.data(list);
	}

	@GetMapping("/getElevatorDetailScreen")
	@ApiOperation("大屏-电梯情况统计板块")
	public R<ElevatorDetailScreenVO> getElevatorDetailScreen() {
		return R.data(maintainPlanContentService.getElevatorDetailScreen());
	}
}
